package cn.oms.auth.provider.security;

import cn.oms.auth.provider.interceptor.AuthInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

//使用SpringMvc框架功能配置跨域
@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Autowired
    private AuthInterceptor authInterceptor;

    @Override
    public void addCorsMappings(CorsRegistry registry) {
            registry.addMapping("/**")
                    .allowedOrigins("*")
                    .allowedMethods("*")
                    .allowedHeaders("*");
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(authInterceptor)
                .addPathPatterns(
                        "/v2/questions",        //学生发布问题
                        "/v2/questions/my",     //学生首页问题列表
                        "/v2/questions/teacher",//讲师首页问题列表
                        "/v2/answers",          //新增回答
                        "/v2/comments",         //新增评论
                        "/v2/comments/*/delete",  // 删除评论
                        "/v2/comments/*/update",  // 修改评论
                        "/v2/answers/*/solved"    // 采纳回答
                );
    }
}
